package com.wnxy.admin.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.wnxy.admin.common.ResponseResult;
import com.wnxy.admin.dto.JSharingManagementDto;
import com.wnxy.admin.entity.ChargingPile;
import com.wnxy.admin.entity.ChargingRules;
import com.wnxy.admin.service.IChargingRulesService;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.wnxy.admin.entity.SharingManagement;
import com.wnxy.admin.service.ISharingManagementService;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 共享管理表 前端控制器
 * </p>
 *
 * @author All
 * @since 2023-11-29
 */
@CrossOrigin
@RestController
@RequestMapping("/sharing-management")
public class SharingManagementController {

    private final ISharingManagementService sharingManagementServiceImpl;
    private final IChargingRulesService chargingRulesServiceImpl;
    public SharingManagementController(ISharingManagementService sharingManagementServiceImpl, IChargingRulesService chargingRulesServiceImpl){
        this.sharingManagementServiceImpl = sharingManagementServiceImpl;
        this.chargingRulesServiceImpl = chargingRulesServiceImpl;
    }

    @GetMapping("/all")
    public ResponseResult<List<ChargingRules>> all(){
        List<ChargingRules> list = chargingRulesServiceImpl.list();
        return ResponseResult.ok(list);
    }

    @PostMapping("/findByPage")
    public ResponseResult<PageInfo<JSharingManagementDto>> findByPage(@RequestBody Map<String,Object> params){
        String chargingPileName = (String) params.get("chargingPileName");
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        //2.调用业务层
        PageInfo<JSharingManagementDto> pageInfo = sharingManagementServiceImpl.findByPage(chargingPileName,pageNum,pageSize);
        //3.返回结果
        return ResponseResult.ok(pageInfo);
    }

    @PostMapping("/save")
    public ResponseResult save(@RequestBody SharingManagement sharingManagement){
        sharingManagement.setSetTime(LocalDateTime.now());
        sharingManagement.setSharedTimeState(1);
        sharingManagementServiceImpl.save(sharingManagement);
        return ResponseResult.ok();
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody SharingManagement sharingManagement){
        sharingManagementServiceImpl.updateById(sharingManagement);
        return ResponseResult.ok();
    }

    @GetMapping("/delete")
    public ResponseResult delete(Long id){
        sharingManagementServiceImpl.removeById(id);
        return ResponseResult.ok();
    }

    @PostMapping("/deleteBatch")
    public ResponseResult deleteBatch(@RequestBody String[] ids){
        sharingManagementServiceImpl.removeBatchByIds(Arrays.asList(ids));
        return ResponseResult.ok();
    }

    @PostMapping("/unshared")
    public ResponseResult unshared(Long id){
        LambdaUpdateWrapper<SharingManagement> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SharingManagement::getId,id)
                .set(SharingManagement::getSharedTimeState, 2)
                .eq(SharingManagement::getSharedTimeState, 1);
        sharingManagementServiceImpl.update(wrapper);
        return ResponseResult.ok();
    }
}
